package com.linwen.javase.thread;

/**
 * wait(),notify(),notifyAll()
 * 这三个方法必须使用在同步代码块或者同步方法中；
 * 且这三个方法的调用者必须是同步代码块或者同步方法中的同步监视器，默认是this
 *
 */

/**
 * @author : linwen
 * @date : 下午9:33 2021/2/22
 */
public class 线程通信1 {
    public static void main(String[] args) {
        Number number = new Number();
        Thread thread1 = new Thread(number);
        Thread thread2 = new Thread(number);
        thread1.setName("线程1");
        thread2.setName("线程2");
        thread1.start();
        thread2.start();
    }
}
class Number implements Runnable{
    private int number = 0;

    @Override
    public synchronized void run() {
        while (true){
            try{
                //默认是当前对象
                this.notify();
                if(number<100){
                    System.out.println(Thread.currentThread().getName()+":"+number);
                    number++;
                    wait();
                }else{
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {

            }

        }
    }
}
